package com.example.wisdomclassroomapi.facade.controller.api;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.wisdomclassroomapi.application.service.RecordStudentLearnApplicationService;
import com.example.wisdomclassroomapi.common.dto.PageDto;
import com.example.wisdomclassroomapi.common.util.TimeUtil;
import com.example.wisdomclassroomapi.domain.entity.RecordStudentLearnEntity;
import com.example.wisdomclassroomapi.facade.assembler.RecordStudentLearnFacadeAssembler;
import com.example.wisdomclassroomapi.facade.controller.common.BaseController;
import com.example.wisdomclassroomapi.facade.dto.RecordStudentLearnCountResponseDto;
import com.example.wisdomclassroomapi.facade.dto.RecordStudentLearnRequestDto;
import com.example.wisdomclassroomapi.facade.dto.RecordStudentLearnResponseDto;
import com.example.wisdomclassroomapi.infrastructure.mapper.RecordStudentLearnMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @Package: com.example.wisdomclassroomapi.facade.controller.wxapp
 * @Description: <前端控制器>-相关接口
 * @Author: scholes
 * @CreateDate: 2022-04-19
 * @UpdateUser: scholes
 * @UpdateDate: 2022-04-19
 * @UpdateRemark: <>
 * @Version: 1.0
 */
@RestController
@RequestMapping("/api/recordStudentLearn")
@Slf4j
public class RecordStudentLearnController extends BaseController {
    @Resource
    private RecordStudentLearnApplicationService recordStudentLearnApplicationService;

    @Resource
    private RecordStudentLearnMapper recordStudentLearnMapper;

    @Resource
    private RecordStudentLearnFacadeAssembler recordStudentLearnFacadeAssembler;

    /**
     * 分页查询
     */
    @PostMapping("/page")
    public PageDto<List<RecordStudentLearnResponseDto>> getRecordStudentLearnByPage(@RequestBody @Valid RecordStudentLearnRequestDto request) {
        IPage<RecordStudentLearnEntity> page = new Page<RecordStudentLearnEntity>();
        page.setSize(request.getPageSize());
        page.setCurrent(request.getPageNum());
        RecordStudentLearnEntity entity = recordStudentLearnFacadeAssembler.assembleRecordStudentLearnEntity(request);
        page = recordStudentLearnApplicationService.page(entity, page);
        List<RecordStudentLearnResponseDto> assembleData = new ArrayList<>();
        if (page != null && page.getRecords() != null && page.getRecords().size() > 0) {
            for (RecordStudentLearnEntity item : page.getRecords()) {
                assembleData.add(recordStudentLearnFacadeAssembler.assembleRecordStudentLearnResponseDto(item));
            }
        }
        PageDto<List<RecordStudentLearnResponseDto>> result = new PageDto<>();
        result.setPageSize((int) page.getSize());
        result.setPageNum((int) page.getCurrent());
        result.setData(assembleData);
        return result;
    }


    /**
     * 查询
     */
    @PostMapping("/list")
    public List<RecordStudentLearnResponseDto> getRecordStudentLearns(@RequestBody @Valid RecordStudentLearnRequestDto request) {
        RecordStudentLearnEntity entity = recordStudentLearnFacadeAssembler.assembleRecordStudentLearnEntity(request);
        List<RecordStudentLearnEntity> list = recordStudentLearnApplicationService.list(entity);
        List<RecordStudentLearnResponseDto> assembleData = new ArrayList<>();
        if (list != null && list.size() > 0) {
            for (RecordStudentLearnEntity item : list) {
                assembleData.add(recordStudentLearnFacadeAssembler.assembleRecordStudentLearnResponseDto(item));
            }
        }
        return assembleData;
    }

    /**
     * 获取某一实体
     */
    @PostMapping("/getRecordStudentLearn")
    public RecordStudentLearnResponseDto getRecordStudentLearnDetails(String id) {
        RecordStudentLearnEntity entity = recordStudentLearnApplicationService.findById(id);
        RecordStudentLearnResponseDto responseDto = null;
        if (entity != null) {
            responseDto = recordStudentLearnFacadeAssembler.assembleRecordStudentLearnResponseDto(entity);
        }
        return responseDto;
    }

    /**
     * 新增数据
     */
    @PostMapping("/add")
    public void saveRecordStudentLearn(@RequestBody RecordStudentLearnRequestDto request) {
        RecordStudentLearnEntity entity = recordStudentLearnFacadeAssembler.assembleRecordStudentLearnEntity(request);
        recordStudentLearnApplicationService.add(entity);
    }

    /**
     * 修改数据
     */
    @PostMapping("/update")
    public void modifyRecordStudentLearn(@RequestBody RecordStudentLearnRequestDto request) {
        RecordStudentLearnEntity entity = recordStudentLearnFacadeAssembler.assembleRecordStudentLearnEntity(request);
        recordStudentLearnApplicationService.updateById(entity);
    }

    /**
     * 删除数据
     */
    @PostMapping("/delete")
    public void removeRecordStudentLearn(@RequestBody @Valid String id) {
        recordStudentLearnApplicationService.deleteById(id);
    }

    @PostMapping("/count")
    public List<RecordStudentLearnCountResponseDto> getRecordStudentCount() {
        String userId = this.getLoginUserId();

        List<RecordStudentLearnCountResponseDto> result = new ArrayList<>();
        LocalDateTime time = TimeUtil.weekStartTime();
        LocalDateTime tmp = time.plusDays(1);
        for (int i = 0; i < 7; i++) {
            RecordStudentLearnCountResponseDto recordStudentLearnCountResponseDto = new RecordStudentLearnCountResponseDto();

            QueryWrapper<RecordStudentLearnEntity> count1 = new QueryWrapper<RecordStudentLearnEntity>()
                    .eq("sutdent_id", userId)
                    .inSql("subject_id", "select id from course_subject where type = \"00\"")
                    .between("create_time", time, tmp);
            Long num1 = recordStudentLearnMapper.count(count1);
            recordStudentLearnCountResponseDto.setCount1(num1 == null ? 0 : num1);

            QueryWrapper<RecordStudentLearnEntity> count2 = new QueryWrapper<RecordStudentLearnEntity>()
                    .eq("sutdent_id", userId)
                    .inSql("subject_id", "select id from course_subject where type = \"01\"")
                    .between("create_time", time, tmp);
            Long num2 = recordStudentLearnMapper.count(count2);
            recordStudentLearnCountResponseDto.setCount2(num2 == null ? 0 : num2);

            QueryWrapper<RecordStudentLearnEntity> count3 = new QueryWrapper<RecordStudentLearnEntity>()
                    .eq("sutdent_id", userId)
                    .inSql("subject_id", "select id from course_subject where type = \"10\"")
                    .between("create_time", time, tmp);
            Long num3 = recordStudentLearnMapper.count(count3);
            recordStudentLearnCountResponseDto.setCount3(num3 == null ? 0 : num3);

            QueryWrapper<RecordStudentLearnEntity> count4 = new QueryWrapper<RecordStudentLearnEntity>()
                    .eq("sutdent_id", userId)
                    .inSql("subject_id", "select id from course_subject where type = \"11\"")
                    .between("create_time", time, tmp);
            Long num4 = recordStudentLearnMapper.count(count4);
            recordStudentLearnCountResponseDto.setCount4(num4 == null ? 0 : num4);

            result.add(recordStudentLearnCountResponseDto);

            time = time.plusDays(1);
            tmp = tmp.plusDays(1);
        }

        return result;

    }

}
